#include "plugin.h"

// ----------------------------------------------------------------------------
// --- image filter plugin ----------------------------------------------------
// ----------------------------------------------------------------------------

#define ID_GLSE LWID_('G','L','S','E')
#define ID_REPO LWID_('R','E','P','O')
#define ID_WRTR LWID_('W','R','T','R')

static LWBlockIdent bwRootBlockIDs[] = {
    ID_GLSE, "GlobalSettings",
    ID_REPO, "RenderParticlesOnly",  
    ID_WRTR, "WriteTrace",
    0
};

// ----------------------------------------------------------------------------
XCALL_(LWError) BWLoad(BWInstanceData *instance, const LWLoadState *ls) 
{ 
    LWID id;

    if (LWLOAD_FIND(ls, bwRootBlockIDs) == ID_GLSE)
    {               
        while (id = LWLOAD_FIND(ls, bwRootBlockIDs))
        {
            switch (id)
            {
            case ID_REPO:
                LWLOAD_I4(ls, &instance->renderParticlesOnly, 1);            
                break;
            case ID_WRTR:
                LWLOAD_I4(ls, &instance->writeTrace, 1);
                break;            
            }

            LWLOAD_END(ls);
        }
    }
    LWLOAD_END(ls);

    EnableTrace(instance->writeTrace);

    return NULL; 
}

// ----------------------------------------------------------------------------
XCALL_(LWError) BWSave(BWInstanceData *instance, const LWSaveState *ss) 
{ 
    // we have to insert a root block, otherwise lightwave will read over
    // the end of the block into the next instance!
    LWSAVE_BEGIN(ss, &bwRootBlockIDs[0], FALSE);

    LWSAVE_BEGIN(ss, &bwRootBlockIDs[1], TRUE);
        LWSAVE_I4(ss, &instance->renderParticlesOnly, 1);
    LWSAVE_END(ss);

    LWSAVE_BEGIN(ss, &bwRootBlockIDs[2], TRUE);
        LWSAVE_I4(ss, &instance->writeTrace, 1);
    LWSAVE_END(ss);
    
    LWSAVE_END(ss);

    return NULL; 
}

// ----------------------------------------------------------------------------
// --- custom object plugin ---------------------------------------------------
// ----------------------------------------------------------------------------

#define ID_RESE LWID_('R','E','S','E')
#define ID_BRFN LWID_('B','R','F','N')
#define ID_PSIZ LWID_('P','S','I','Z')
#define ID_COVA LWID_('C','O','V','A')
#define ID_CODI LWID_('C','O','D','I')
#define ID_PDEN LWID_('P','D','E','N')
#define ID_FIPL LWID_('F','I','P','L')
#define ID_PALI LWID_('P','A','L','I')
#define ID_BFCU LWID_('B','F','C','U')

static LWBlockIdent bwCoRootBlockIDs[] = {
    ID_RESE, "RenderSettings",
    ID_BRFN, "BrushFilename",
    ID_PSIZ, "ParticleSize",  
    ID_COVA, "ColorVariation",
    ID_CODI, "ColorDisplacement",
    ID_PDEN, "ParticleDensity",
    ID_FIPL, "FixedParticlePlacement",
    ID_PALI, "AlignParticles",
    ID_BFCU, "BackfaceCulling",
    0
};

// ----------------------------------------------------------------------------
XCALL_(LWError) BWcoLoad(BWcoInstanceData *instance, const LWLoadState *ls) 
{ 
    LWID id;

    if (LWLOAD_FIND(ls, bwCoRootBlockIDs) == ID_RESE)
    {               
        while (id = LWLOAD_FIND(ls, bwCoRootBlockIDs))
        {
            switch (id)
            {
            case ID_PSIZ:
                LWLOAD_FP(ls, &instance->renderSettings.partSize, 1);            
                break;
            case ID_BRFN:
                LWLOAD_STR(ls, instance->renderSettings.brushFilename, 
                    MAX_FILENAME_LENGTH);
                break;            
            case ID_COVA:
                LWLOAD_FP(ls, &instance->renderSettings.colorVariation, 1);
                break;
            case ID_CODI:
                LWLOAD_FP(ls, &instance->renderSettings.colorDisplacement, 1);
                break;
            case ID_PDEN:
                LWLOAD_FP(ls, &instance->renderSettings.partDensity, 1);
                break;
            case ID_FIPL:
                LWLOAD_I4(ls, &instance->renderSettings.fixedPartPlacement, 1);
                break;
            case ID_PALI:
                LWLOAD_I4(ls, &instance->renderSettings.alignParticles, 1);
                break;
            case ID_BFCU:
                LWLOAD_I4(ls, &instance->renderSettings.backFaceCulling, 1);
                break;
            }
            LWLOAD_END(ls);
        }
    }
    LWLOAD_END(ls);

    return NULL; 
}

// ----------------------------------------------------------------------------
XCALL_(LWError) BWcoSave(BWcoInstanceData *instance, const LWSaveState *ss) 
{ 
    // we have to insert a root block, otherwise lightwave will read over
    // the end of the block into the next instance!
    LWSAVE_BEGIN(ss, &bwCoRootBlockIDs[0], FALSE);

    LWSAVE_BEGIN(ss, &bwCoRootBlockIDs[1], TRUE);
        LWSAVE_STR(ss, instance->renderSettings.brushFilename);
    LWSAVE_END(ss);

    LWSAVE_BEGIN(ss, &bwCoRootBlockIDs[2], TRUE);
        LWSAVE_FP(ss, &instance->renderSettings.partSize, 1);
    LWSAVE_END(ss);

    LWSAVE_BEGIN(ss, &bwCoRootBlockIDs[3], TRUE);
        LWSAVE_FP(ss, &instance->renderSettings.colorVariation, 1);
    LWSAVE_END(ss);

    LWSAVE_BEGIN(ss, &bwCoRootBlockIDs[4], TRUE);
        LWSAVE_FP(ss, &instance->renderSettings.colorDisplacement, 1);
    LWSAVE_END(ss);

    LWSAVE_BEGIN(ss, &bwCoRootBlockIDs[5], TRUE);
        LWSAVE_FP(ss, &instance->renderSettings.partDensity, 1);
    LWSAVE_END(ss);

    LWSAVE_BEGIN(ss, &bwCoRootBlockIDs[6], TRUE);
        LWSAVE_I4(ss, &instance->renderSettings.fixedPartPlacement, 1);
    LWSAVE_END(ss);

    LWSAVE_BEGIN(ss, &bwCoRootBlockIDs[7], TRUE);
        LWSAVE_I4(ss, &instance->renderSettings.alignParticles, 1);
    LWSAVE_END(ss);

    LWSAVE_BEGIN(ss, &bwCoRootBlockIDs[8], TRUE);
        LWSAVE_I4(ss, &instance->renderSettings.backFaceCulling, 1);
    LWSAVE_END(ss);
    
    LWSAVE_END(ss);

    return NULL; 
}